package em.ctrl;

import base.ctrl.BaseController;
import base.jfinal.anatation.RouteBind;
import com.jfinal.aop.Before;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import em.model.Logger;
import em.model.Org;
import em.util.CommonTools;

import java.util.*;

@RouteBind(path = "OrgManage")
public class OrgCtrl extends BaseController {

    /**
     * 根据ID查找组织
     * @return 组织信息
     */
    public void  getOrgById() {
        Org org=Org.dao.findById(getPara("orgId"));
        org.put("OriginalName",org.getStr("name"));
        org.put("pidName",Db.findFirst("select `Name` from em_org where OrgId=?",org.getInt("pid")).get("Name"));
        renderJson(org);
    }

    /**
     * 修改组织
     * @pram 组织json
     * @return err:0成功 err:1名称重复
     */
    public void modifiyOrg() {
        Org org=Org.dao.findFirst("select `Name`,OrgId from em_org where `Name`=?",getPara("name"));
        if(null!=org && org.getInt("OrgId")!=getParaToInt("orgId")){
            renderJson("{\"err\":1}");//组织名重复
            return;
        }
        Map<String,Object> map=getParas();
        map.put("updateBy",getParaToInt("GlobalAccId"));
        map.put("updateDate",new Date());
        Org.dao.findById(getPara("orgId")).setValues(map).update();
        new Logger().set("AccountId",getParaToInt("GlobalAccId")).set("Model", "组织管理").set("Operation", "修改组织")
                    .set("Desc", "组织名称:" + getPara("name")).set("SoureIp", this.getRequest().getRemoteAddr()).save();
        renderJson("{\"err\":0}");
    }

    /**
     * 创建组织
     * @return err:0成功 err:1名称重复
     */
    @Before(Tx.class)
    public void createOrg() {
        Map<String,Object> map=getParas();
        map.put("createBy",getParaToInt("GlobalAccId"));
        map.put("createDate",new Date());
        String strName=Db.queryStr("select name from em_org where `name`=? and pid=?",getPara("name"),getPara("pid"));
        if(StrKit.notBlank(strName)){
            renderJson("{\"err\":1}");//新建组织同名
        }else{
            //生成组织6位随机码
            //String strRandom=ShareFun.getInstance().createRandomCode();
            Org org=new Org();
            //map.put("org_radom_code",strRandom);
            org.setValues(map).remove("orgId").save();
            //ShareFun.getInstance().insertOrgRealation(org.getInt("OrgId"),getPara("name"));
            new Logger().set("AccountId",getParaToInt("GlobalAccId")).set("Model", "组织管理").set("Operation", "创建组织")
                    .set("Desc", "组织名称:" + getPara("name")).set("SoureIp", this.getRequest().getRemoteAddr()).save();
            renderJson("{\"err\":0}");
            }
    }
    /**
     * 启用组织
     */
    public void enabledOrg() {
        Org.dao.findById(getPara("OrgId")).set("status",0).update();
        new Logger().set("AccountId",getParaToInt("GlobalAccId")).set("Model", "组织管理").set("Operation", "启用组织")
                .set("Desc", "组织名称:" + getPara("orgName")).set("SoureIp", this.getRequest().getRemoteAddr()).save();
        renderJson("{\"err\":0}");
    }
    /**
     * 禁用组织
     */
    public void disabledOrg() {
        Org.dao.findById(getPara("OrgId")).set("status",1).update();
        Db.update("UPDATE em_account set `status`='1' where orgid=?",getPara("orgId"));

        new Logger().set("AccountId",getParaToInt("GlobalAccId")).set("Model", "组织管理").set("Operation", "禁用组织")
                .set("Desc", "组织名称:" + getPara("orgName")).set("SoureIp", this.getRequest().getRemoteAddr()).save();
        renderJson("{\"err\":0}");
    }
    /**
     * 根据父级ID查询所有子组织
     * @param ;
     * @return 组织集合
     */
    public void getOrgListByPid() {
        List<Record> list=getData("org.getOrgListByPid",getParas());
        renderJson(list);
    }

    public void getOrgNameList() {
        List<Record> list=Db.find(CommonTools.getInstance().getSql("org.getOrgNameList"));
        HashMap<String,Object> map= new  HashMap<String,Object>();
        map.put("list", list);
        renderJson(map);
    }
    public void getOrgTreeByPid(){
        List<Record> orgs=Db.find("SELECT o.orgId,`name`,pid,node FROM em_org o JOIN em_org_relation r ON o.OrgId=r.ChildOrgId\n" +
                "WHERE r.OrgId=? ORDER BY node,o.orgId",getPara("GlobalOrgId"));
        renderJson(orgs);
    }
}
